/* 
 * File:   Controller.cpp
 * Author: chb
 * 
 * Created on 4. September 2009, 21:00
 */

#include "Controller.h"
#include <unistd.h>

Controller* Controller::_instance = 0;

Controller::Controller() {
    _running = true;

}

Controller* Controller::instance() {
    if (_instance == 0)
        _instance = new Controller;
    return _instance;
}

void Controller::mainLoop() {
    SDL_Event e;

    while (_running) {
        while (SDL_PollEvent(&e)) {
            switch (e.type) {
                case SDL_KEYDOWN:
                    keyDownEvent(e);
                    break;
                case SDL_QUIT:
                    _running = false;
                    break;
            }
        }
        usleep(100);

    }

}

void Controller::keyDownEvent(SDL_Event& e) {
    switch (e.key.keysym.sym){
        case SDLK_ESCAPE:
            _running = false;
            break;
        case SDLK_LEFT:
            _hero->moveLeft();      
            break;
        case SDLK_RIGHT:
            _hero->moveRight();
            break;
        case SDLK_UP:
            _hero->moveUp();
            break;
        case SDLK_DOWN:
            _hero->moveDown();
            break;
    }

}

void Controller::setHero(Hero* h){
    _hero = h;
}

Controller::~Controller() {
    // TODO Auto-generated destructor stub
}

